package com.itheima.store.controller;


import com.itheima.store.entity.Category;
import com.itheima.store.entity.PageBean;
import com.itheima.store.entity.Product;
import com.itheima.store.service.CategoryService;
import com.itheima.store.service.ProductService;
import com.itheima.store.utils.Result;
import com.itheima.store.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class AdminController {


    @Autowired
    CategoryService categoryService;
    @Autowired
    ProductService productService;


    @RequestMapping("/admin")
    public Result findAllCategory(String method,String cid,String cname,String currentPage){
        if("findAllCategory".equals(method)){
            List<Category> categoryList = categoryService.findAll();
            if(categoryList != null){
                return new Result(1,"查询成功",categoryList);
            }
            return new Result(0,"查询失败");
        }
        if("deleteCategoryById".equals(method)){

            if(categoryService.deleteCategory(cid)){
                return new Result(Result.SUCCESS,"删除成功");
            }
            return new Result(Result.FAILS,"删除失败");

        }
        if ("findCategoryById".equals(method)){

            Category category = categoryService.findCategoryById(cid);
            if(category != null){
                return new Result(Result.SUCCESS,"成功",category);
            }
            return new Result(0,"失败");
        }
        if("editCategoryById".equals(method)){

            if (categoryService.editCategoryById(cid, cname)){
                return new Result(1,"修改成功");
            }
            return new Result(0,"修改失败");
        }
        if("addCategory".equals(method)){
            String ccid = UUIDUtils.getUUID();
            if(categoryService.addCategory(cname,ccid)){
                return new Result(1,"添加成功");
            }
            return new Result(0,"添加失败");
        }
        if("findProductByPage".equals(method)){

            PageBean<Product> pageBean = productService.findByPage(Integer.parseInt(currentPage),10);
            if(pageBean != null){

                return new Result(1,"成功",pageBean);
            }
            return new Result(0,"失败");
        }
        return null;
    }
}
